package com.guigu.lc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.common.mapper.SysRolesMapper;
import com.guigu.common.mapper.SysUserRoleMapper;
import com.guigu.common.mapper.SysUsersMapper;
import com.guigu.common.pojo.SysRoles;
import com.guigu.common.pojo.SysUsers;
import com.guigu.common.vo.SearchVo;
import com.guigu.lc.serach.UserSerach;
import com.guigu.lc.service.UserService;
import com.guigu.lc.view.usergentroleview;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import javax.annotation.Resource;
import java.util.List;

@Service
public class UserServiceimpl implements UserService {

    @Resource
    private SysUsersMapper sysUsersMapper;

    @Resource
    private SysRolesMapper sysRolesMapper;

    @Resource
    private SysUserRoleMapper sysUserRoleMapper;

    @Override
    public UserSerach queryalluserService(UserSerach searchVo) {
        PageHelper.startPage(searchVo.getPageNumber(),searchVo.getPageSize());
        PageInfo pageInfo=new PageInfo(this.sysUsersMapper.queryalluser(searchVo));
        UserSerach searchVo1=new UserSerach();
        searchVo1.setRows(pageInfo.getList());
        searchVo1.setTotal(pageInfo.getTotal());
        return searchVo1;
    }

    @Override
    public usergentroleview selectusergentroleService(int loginid, int gentid) {
        usergentroleview view=new usergentroleview();
        //查询当前登录账号用于的角色
        view.setSysroles(this.sysRolesMapper.querroleByuserid(loginid));
        //查询授权账号拥有的角色
        List<SysRoles> sysRoles = this.sysRolesMapper.querroleByuserid(gentid);
        StringBuffer targetaccountstr=new StringBuffer();
        for(SysRoles r:sysRoles) {
            targetaccountstr.append(r.getId()).append(",");
        }
        view.setS(targetaccountstr);
        return view;
    }

    @Override
    public void grantwithdrawuserroleService(usergentroleview usergentroleview) {
        this.sysUserRoleMapper.deleteuserid(usergentroleview);
        this.sysUserRoleMapper.insertgentroleuserid(usergentroleview);

    }

    @Override
    public void updateloginByidService(SysUsers users) {
        this.sysUsersMapper.updateByid(users);
    }

    @Override
    public void updatepasswordsys(SysUsers users) {
        this.sysUsersMapper.updatepasswordByid(users);
    }

    @Override
    public void insertuserzcService(SysUsers users) {
        this.sysUsersMapper.insertuserzc(users);
    }
}
